package com.nolon.flange.view

import android.content.Context
import android.database.Cursor
import android.provider.MediaStore
import com.nolon.flange.model.FileItem
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList


/**
 * 获取所有文件
 */
fun getFilesByType(context: Context): List<FileItem>? {
    val files: MutableList<FileItem> = ArrayList()
    // 扫描files文件库
    var c: Cursor? = null
    try {
        val mContentResolver = context.getContentResolver()
        c = mContentResolver.query(
            MediaStore.Files.getContentUri("external"),
            null,
            null,
            null,
            null
        )
        val columnIndexOrThrow_ID: Int = c!!.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)
        val columnIndexOrThrow_MIME_TYPE: Int =
            c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE)
        val columnIndexOrThrow_DATA: Int =
            c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)
        val columnIndexOrThrow_SIZE: Int =
            c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE)
        // 更改时间
        val columnIndexOrThrow_DATE_MODIFIED: Int =
            c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED)
        var tempId = 0
        while (c.moveToNext()) {
            val path: String = c.getString(columnIndexOrThrow_DATA)
            val minType: String = c.getString(columnIndexOrThrow_MIME_TYPE)
            val position_do = path.lastIndexOf(".")
            if (position_do == -1) {
                continue
            }
            val position_x: Int = path.lastIndexOf(File.separator)
            if (position_x == -1) {
                continue
            }
            val displayName = path.substring(position_x + 1, path.length)
            val size: Long = c.getLong(columnIndexOrThrow_SIZE)
            val modifiedDate: Long = c.getLong(columnIndexOrThrow_DATE_MODIFIED)
            val file = File(path)
            val time: String =
                SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(file.lastModified()))
//            val info = FileItem(displayName, path)
//            files.add(info)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        c?.close()
    }
    return files
}

//private fun listDocs(context: Context): ArrayList<FileItem>? {
//    val docs: ArrayList<FileItem> = arrayListOf()
//    val projection = arrayOf(
//        MediaStore.Files.FileColumns._ID,
//        MediaStore.Files.FileColumns.DATA,
//        MediaStore.Files.FileColumns.TITLE,
//        MediaStore.Files.FileColumns.MIME_TYPE,
//        MediaStore.Files.FileColumns.SIZE,
//        MediaStore.Files.FileColumns.DATE_MODIFIED
//    )
//
//    val cursor: Cursor? = context.contentResolver.query(
//        MediaStore.Files.getContentUri("external"),
//        projection,
//        null,
//        null,
//        null
//    )
//    if (cursor == null) return docs else if (cursor.count > 0 && cursor.moveToFirst()) {
//        do {
//            val path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA))
//            val title = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE))
//            if (path != null
//                && (path.endsWith(".xls"))
//            ) {
//                val strings: HybridFileParcelable =
//                    RootHelper.generateBaseFile(File(path), showHiddenFiles)
//                if (strings != null) {
//                    val parcelable: LayoutElementParcelable = createListParcelables(strings)
//                    if (parcelable != null) docs.add(parcelable)
//                }
//            }
//        } while (cursor.moveToNext())
//    }
//    cursor.close()
//    docs.sortBy { it.modifiedDate }
//    return docs
//}